import { defineStore } from "pinia";
export const useTabsStore = defineStore('tabs',{
    state:()=>({
         tabsList:[
             {
                 path:"/dashboard",
                 name:"Dashboard",
                 title:"首页"
             }
         ],
         activeTab:"/dashboard"
    }),
    actions:{
        //添加标签页
        addTab(tab){
            const isExist = this.tabsList.some(item=>item.path === tab.path);
            if(!isExist){
                this.tabsList.push(tab);
            }
            this.activeTab = tab.path;
        },
        //移除标签页
        removeTab(path){
            //不能删除最后一个标签页
            if(this.tabsList.length<=1) return;

            const index = this.tabsList.findIndex(item=>item.path ===path);

            //如果删除的是当前激活的标签页，需要切换到其他标签
            if(this.activeTab ===path){
                //如果是最后一个标签，切换到前一个
                if(index===this.tabsList.length-1){
                    this.activeTab = this.tabsList[index-1].path;
                }else{
                    //否则切换到最后一个
                    this.activeTab = this.tabsList[index+1].path;
                }
            }
            this.tabsList.splice(index,1);
        },
        //切换标签页
        changeTab(path){
            this.activeTab = path;
        }
    }
})